package cn.edu.klmy.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import web.models.CommonResult;
import web.models.Page;
import cn.edu.klmy.dao.ICourseDao;
import cn.edu.klmy.models.course.Course;

@Controller
@RequestMapping("/course")
public class CourseController {

	@Resource(name = "courseDao")
	private ICourseDao courseDao;

	@RequestMapping("/add")
	@ResponseBody
	public CommonResult addCourse(@ModelAttribute Course course) {
		courseDao.addCourse(course);
		return new CommonResult(new Boolean(true));
	}

	@RequestMapping("/update")
	@ResponseBody
	public CommonResult updateCourse(@ModelAttribute Course course) {
		courseDao.updateCourse(course);
		return new CommonResult(new Boolean(true));
	}

	@RequestMapping("/remove")
	@ResponseBody
	public CommonResult removeCourse(@RequestParam int courseId) {
		courseDao.removeCourse("ID", courseId);
		return new CommonResult(new Boolean(true));
	}

	@RequestMapping("/exists")
	@ResponseBody
	public CommonResult courseExists(@RequestParam String courseName) {
        boolean result = courseDao.courseExists(courseName);
        return new CommonResult(result);
	}

	@RequestMapping("/all")
	@ResponseBody
	public Page<Course> getCoursesPage(@RequestParam int page,
			@RequestParam int rows) {
		List<Course> courses = courseDao.getCourses(null, page, rows);
		int count = courseDao.getCourseCount(null);
		return new Page<Course>(count, courses);
	}

	@RequestMapping("/in-coursestructure/{courseStructureId}")
	@ResponseBody
	public List<Course> getCoursesInCourseStructure(
			@PathVariable int courseStructureId) {
		return courseDao.getCourses("A.course_structure_id", courseStructureId);
	}

	@RequestMapping("/list/all")
	@ResponseBody
	public List<Course> getAllCourses() {
		return courseDao.getAllCourses();
	}

	public void setCourseDao(ICourseDao courseDao) {
		this.courseDao = courseDao;
	}
}
